<?php

namespace App\ZhCN;

if (!defined('IN_PX'))
    exit;

use App\Service;
use App\Repository;
use Phoenix\Log\Log4p as logger;
use App\ZhCN\Api\V1;

/**
 * 首页
 */
class Product {

    private function __Controller() {}

    private function __Value($__PACKAGE__, $__ROOT__, $__RM__, $setting, $cfg, $__LANGUAGE_ID__) {}

    protected function __Inject($session, Service\Look $serLook, Service\Product $servPro, Repository\Product $repoPro) {}

    /**
     * 检查是否登录，并且返回用户ID
     */
    private function checkLogin(){
        $user = $this->session->user;
        $userId = $user != null ? $user['memberId'] : 0;
        if($userId == 0) {
            return 0;
            header("Location: /login");
            exit;
        }
        return $userId;
    }

    private function findMemberId(){
        $user = $this->session->user;
        $userId = $user != null ? $user['memberId'] : 0;
        return $userId;
    }

    public function productType(){
        $type = $this->servPro->productType();
        if ($type) {
            return $type;
        } else {
            return 404;
        }
    }

    public function product($__Route = array('/product/list/{id}/{style}/{scene}/{page}')){
        $_model['type'] = $this->productType();
        $_model['style'] = $this->repoPro->productStyle();
        $_model['scene'] = $this->repoPro->productScene();
        if ($_model['type'] == 404) {
            return 404;
        }
        if ($_model) {

            if (!isset($this->id)) {
                $_model['currentNavType'] = 0;
            } else {
                $_model['currentNavType'] = $this->id;
            }

            if (!isset($this->style)) {
                $_model['currentNavStyle'] = 0;
            } else {
                $_model['currentNavStyle'] = $this->style;
            }

            if (!isset($this->scene)) {
                $_model['currentNavScene'] = 0;
            } else {
                $_model['currentNavScene'] = $this->scene;
            }

            if (!isset($this->page)) {
                $_model['currentPage'] = 1;
            } else {
                $_model['currentPage'] = $this->page;
            }

            $_model['aliasId'] = '/product/list/' . $_model['currentNavType'] . '/' . $_model['currentNavStyle'] . '/' . $_model['currentNavScene'];

            $_model['currentPageSize'] = 50;

            if (!isset($_GET['search']) || $_GET['search'] == '' || $_GET['search'] == null ) {
                $_model['search'] =  0;
            } else {
                $_model['search'] = $_GET['search'];
            }
            $_model['currentCategoryTotal'] = $this->repoPro->findProductCount($_model['currentNavType'], $_model['currentNavStyle'], $_model['currentNavScene'], $_model['search']);


            $_model['product'] = $this->repoPro->findProductAll($_model['currentNavType'], $_model['currentNavStyle'], $_model['currentNavScene'], $_model['search'], (($_model['currentPage'] - 1) * $_model['currentPageSize']), $_model['currentPageSize']);


            return array (
                'model' => $_model,
                'view' => 'product/index'
            );
        }
        return 404;
    }

    public function productDetail($__Route = array('/product/detail/{id:\d+}')) {
        $_model['type'] = $this->productType();
        if ($_model['type'] == 404) {
            return 404;
        }
        if ($_model) {
            $_model['product'] = $this->servPro->findProductDetail($this->id);
            if (!$_model['product']) {
                return 404;
            }
            $_model['stamp'] = microtime(true) * 10000;


            return array (
                'model' => $_model,
                'view' => 'product/show'
            );
        }
        return 404;
    }


}
